﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.UploadFileRecord;
using BCDto.FileManagement;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace BCService.FileManagement
{
    /// <summary>
    /// 图片文件管理服务
    /// </summary>
    public class ImageFileManagementService : IImageFileManagementService
    {
        private string WebsiteDirectory { get; set; }
        private readonly IConfiguration configuration;
        public ImageFileManagementService(IConfiguration configuration,IUploadFileRecordData uploadFileRecordData)
        {
            this.configuration = configuration;
            WebsiteDirectory = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, configuration["WebsiteName"]);
        }

        public IEnumerable<ImageFileManagementDto> GetImageFolder(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                string imageRootDirectory = Path.Combine(this.WebsiteDirectory, "image");
                if (!Directory.Exists(imageRootDirectory))
                {
                    Directory.CreateDirectory(imageRootDirectory);
                }
                DirectoryInfo dir = new DirectoryInfo(imageRootDirectory);
                return GetImageFiles(dir);
            }
            else
            {
                if (!Directory.Exists(path))
                {
                    throw new ArgumentException("此目录并不存在。");
                }
                DirectoryInfo dir = new DirectoryInfo(path);
                return GetImageFiles(dir);
            }
        }

        private IEnumerable<ImageFileManagementDto> GetImageFiles(DirectoryInfo dir)
        {
            DirectoryInfo[] directories = dir.GetDirectories();
            List<ImageFileManagementDto> list = new List<ImageFileManagementDto>();
            foreach (DirectoryInfo directory in directories)
            {
                list.Add(new ImageFileManagementDto
                {
                    FileType = FileType.Folder,
                    FolderName = directory.Name,
                    DirectoryPath = directory.FullName,
                    CreateTime = directory.CreationTime
                });
            }
            if (list.Any())
            {
                list = list.OrderByDescending(p =>p.CreateTime).ToList();
            }
            FileInfo[] files = dir.GetFiles();
            if (files.Any())
            {
                files = files.OrderByDescending(p =>p.CreationTime).ToArray();
            }
            Regex regex = new Regex("[0-9]{2,4}[x]{1}[0-9]{2,4}");
            string websiteHost = configuration["DefaultHost"];
            foreach (FileInfo file in files)
            {
                if (regex.IsMatch(file.Name))
                {
                    continue;
                }
                string fileType = file.Extension.Replace(".", "");
                object obj;
                FileType type;
                string[] imageType = new string[] { ".jpg", ".gif", ".bmp", ".png" };
                if (imageType.Contains(file.Extension))
                {
                    type = FileType.Image;
                }
                else
                {
                    if (!Enum.TryParse(typeof(FileType), fileType, out obj))
                    {
                        continue;
                    }
                    type = (FileType)obj;
                }

                list.Add(new ImageFileManagementDto
                {
                    FileType = type,
                    DirectoryPath = file.FullName,
                    FolderName = file.DirectoryName,
                    FileSuffix = file.Extension,
                    ImageName = file.Name,
                    ImageSize = file.Length,
                    ImageUrl = string.Concat(websiteHost, file.FullName.Replace(this.WebsiteDirectory, "").Replace("\\", "/")),
                    CreateTime = file.CreationTime
                });
            }
            return list;
        }

    }
}
